package com.ruoyi.guarantee.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.guarantee.domain.BAgreement;
import com.ruoyi.guarantee.mapper.BAgreementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.guarantee.mapper.BOrderMapper;
import com.ruoyi.guarantee.domain.BOrder;
import com.ruoyi.guarantee.service.IBOrderService;

/**
 * 询单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-17
 */
@Service
public class BOrderServiceImpl implements IBOrderService
{
    @Autowired
    private BOrderMapper bOrderMapper;
    @Autowired
    private BAgreementMapper agreementMapper;

    /**
     * 查询询单
     *
     * @param id 询单主键
     * @return 询单
     */
    @Override
    public BOrder selectBOrderById(Long id)
    {
        return bOrderMapper.selectBOrderById(id);
    }

    /**
     * 查询询单列表
     *
     * @param bOrder 询单
     * @return 询单
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BOrder> selectBOrderList(BOrder bOrder)
    {
        return bOrderMapper.selectBOrderList(bOrder);
    }


    @Override
    public List<BOrder> selectBOrderCheckList(BOrder bOrder)
    {
        return bOrderMapper.selectBOrderCheckList(bOrder);
    }
    /**
     * 新增询单
     *
     * @param bOrder 询单
     * @return 结果
     */
    @Override
    public int insertBOrder(BOrder bOrder)
    {
        bOrder.setCreateTime(DateUtils.getNowDate());
        return bOrderMapper.insertBOrder(bOrder);
    }

    /**
     * 修改询单
     *
     * @param bOrder 询单
     * @return 结果
     */
    @Override
    public int updateBOrder(BOrder bOrder)
    {
        bOrder.setUpdateTime(DateUtils.getNowDate());
        return bOrderMapper.updateBOrder(bOrder);
    }

    /**
     * 批量删除询单
     *
     * @param ids 需要删除的询单主键
     * @return 结果
     */
    @Override
    public int deleteBOrderByIds(Long[] ids)
    {
        return bOrderMapper.deleteBOrderByIds(ids);
    }

    /**
     * 删除询单信息
     *
     * @param id 询单主键
     * @return 结果
     */
    @Override
    public int deleteBOrderById(Long id)
    {
        return bOrderMapper.deleteBOrderById(id);
    }

    @Override
    public int zhuanBOrderById(Long id) {
        BOrder order= bOrderMapper.selectBOrderById(id);
        BAgreement agreement = new BAgreement();
        agreement.setStatus(2L);
        agreement.setXmType(order.getBbzrQyxz());
        agreement.setQyClDate(DateUtils.parseDate(order.getBbzrDate()));
        agreement.setUserId(order.getUserId());
        agreement.setXmSyName(order.getSyName());
        agreement.setXmDate(order.getXmDate());
        agreement.setXmSyType(order.getSyQyxz());
        agreement.setQyZcMoney(order.getBbzrZczj());
        agreement.setQyZizhi(order.getZgzz());
        agreement.setXmName(order.getXmName());
        agreement.setBhMoney(order.getBhMoney());
        agreement.setCreateBy(order.getCreateBy());
        agreement.setCreateTime(DateUtils.getNowDate());
        agreement.setXmChDjuser(order.getXmChDjuser());
        agreement.setDuijieren(order.getDuijieren());
        agreement.setXmGongqi(order.getXmGongqi());
        agreement.setXmBhqx(order.getDbQixian());
        agreementMapper.insertBAgreement(agreement);

        order.setStatus(6L);
        return bOrderMapper.updateBOrder(order);
    }
}
